'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 铁匠铺列表功能
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var smithyConf = require('../../../config/smithyConf.js');
var unionTechService = require('../../../services/unionTechService.js');
var unionTechType = require('../../../consts/unionTechType.js');
var buildType = require('../../../consts/buildType.js');
var constantUtil = require('../../../util/constantUtil.js');
var techType = require('../../../consts/techType.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 铁匠铺列表
 * PS:  now_open_smithy_list 工匠槽开放列表
 *      now_worker_list 当前工作列表
 *      now_open_worker_list 工人开放列表
 */
Handler.prototype.list = function (msg, session, next) {
    var role_id = session.uid;
    var now_open_smithy_list = [];
    var now_open_worker_list = [];
    var now_worker_list = [];
    var energy = 0;
    var max_energy = 0;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'smithy', 'build', 'bag', 'accrued', 'tech'],
                    cb
                );
            },
            function (cb) {
                unionTechService.getUnionTechValue(player_manager, unionTechType.UNION_TECH_103, cb);
            },
            function (union_tech_value, cb) {
                var role_model = player_manager.role;
                var tech_model = player_manager.tech;
                var smithy_model = player_manager.smithy;
                var build_model = player_manager.build;
                var build = build_model.getBuildByType(buildType.SMITHY);
                var tech_value = tech_model.getEffByTechType(techType.ADD_MAX_SMITHY_ENERGY);
                max_energy = constantUtil.value('energy_max_limit') + union_tech_value + tech_value;

                if (!build) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }
                var build_lv = build.getLv();
                var open_worker_list = smithyConf.getOpenLv(build_lv);

                _.each(open_worker_list, function (value) {
                    //加入建筑等级对应工人开放列表
                    smithy_model.openWorker(value);
                });

                var smithy_list = smithy_model.getSmithyOpenList();
                _.each(smithy_list, function (key) {
                    now_open_smithy_list.push({
                        smithy_id: key,
                        smithy_state: 1
                    });
                });

                var worker_list = smithy_model.getWorkerOpenList();
                _.each(worker_list, function (key) {
                    now_open_worker_list.push({
                        worker_id: key,
                        worker_state: 1
                    });
                });

                energy = smithy_model.getEnergy();

                // 当前精练队列
                now_worker_list = smithy_model.getWorkerSchedule();
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    now_open_smithy: now_open_smithy_list,
                    now_open_worker: now_open_worker_list,
                    now_worker_list: now_worker_list,
                    energy: energy,
                    max_energy: max_energy
                }
            });
        }
    );
};
